<template>
  <div class="_wz_hiddenTroubleplanAdd">
    <div style="padding: 10px 0 40px 0;">
      <Form ref="formAdd" :model="fromData" label-position="right" :label-width="100" :rules="ruleValidate">
        <FormItem label="运输企业" prop="deptUuid">
          <i-select v-model="fromData.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :key="item.uuid" :value="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="计划名称" prop="name">
          <Input v-model="fromData.name" placeholder="请填写计划名称"></Input>
        </FormItem>
        <FormItem label="排查类型" prop="inspectTypeUuid">
          <i-select v-model="fromData.inspectTypeUuid" filterable placeholder="请选择排查类型"  :label-in-value="true"  @on-change="public.selectName($event,fromData,'inspectTypeName')">
            <Option v-for="(item,index) in inspectList" :value="item.uuid" :key="item.index">{{ item.typeName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="开始时间" prop="wzStartDate">
          <DatePicker type="date" placeholder="请选择开始时间" v-model="fromData.wzStartDate"></DatePicker>
        </FormItem>
        <FormItem label="结束时间" prop="wzEndDate">
          <DatePicker type="date" placeholder="请选择结束时间" v-model="fromData.wzEndDate"></DatePicker>
        </FormItem>
      </Form>
    </div>
    <div class="demo-drawer-footer" style="z-index: 99;">
      <Button style="margin-right: 8px" @click="$emit('closeFun', false)">取消</Button>
      <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
    </div>
  </div>
</template>
<script>
export default {
  props: ['selectData'],//接收来自父组件的数据
  data() {
    return {
      fromData: {},
      ruleValidate: {//正则
        deptUuid: [
          {required: true, message: '请选择运输企业', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请填写计划名称', trigger: 'blur'}
        ],
        inspectTypeUuid: [
          {required: true, message: '请选择排查类型', trigger: 'blur'}
        ],
        wzStartDate: [
          {type:'date',required: true, message: '请选择开始时间', trigger: 'blur'}
        ],
        wzEndDate: [
          {type:'date',required: true, message: '请填写计划名称', trigger: 'blur'}
        ],
      },
      inspectList:[],
      subDeptList: [],//运输企业
    }
  },
  computed: {//计算属性

  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态

  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态
    var that = this


    //运输企业
    that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
      if (!!res) {
        that.subDeptList = res.data.data;
        if(!that.selectData.id){
          that.fromData.deptUuid = that.subDeptList[0].uuid
        }
      }
    }).catch(err => {
      console.log("失败", err)
    })


    that.fromData = that.selectData;

    that.fromData.wzStartDate = !!that.fromData.startDate ? new Date(that.fromData.startDate):'';
    that.fromData.wzEndDate = !!that.fromData.endDate ? new Date(that.fromData.endDate):'';

    //隐患排查类型
    that.axios.post(that.apiUrl.webApi + "/safety/risk/riskInspectTypeSettingList", {}).then(res => {
      if (!!res) {
        that.inspectList = res.data.data;
      }
    }).catch(err => {
      console.log("失败", err)
    })
  },
  methods: {//执行的方法
    //提交表单
    addSubmit(data) {
      let that = this;

      that.$refs[data].validate((valid) => {
        if (valid) {
          that.fromData.startDate = that.public.timeFormatting(that.fromData.wzStartDate, "yyyy-MM-dd")
          that.fromData.endDate = that.public.timeFormatting(that.fromData.wzEndDate, "yyyy-MM-dd")
          that.spinShow = true;//显示加载
          that.axios.post(that.apiUrl.webApi + "/safety/risk/riskPlanSaveOrUpdate", that.fromData).then(res => {
            that.spinShow = false;//显示加载
            if (!!res) {
              that.$Message.success("保存成功");
              that.$emit("closeFun", true)
            }
          }).catch(err => {
            console.log('失败', err);
          })

        } else {
          this.$Message.error('请正确填写表单');
        }
      })
    },
  },
  watch: {//监听

  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">
  ._wz_hiddenTroubleplanAdd{

  }
</style>
